/**
 * @Controller 公共接口
 */
const Controller = require('egg').Controller;

class PublicController extends Controller {
    /**
     * @Router GET /api/v1/public/templates
     * @Request query number page 当前页（1）
     * @Request query number pageSize 分页大小（10）
     * @Response 200 createTemplatesResponse 响应
     * @Summary 获取首页模板列表
     */
    async getTemplates() {
        const ctx = this.ctx;
        try {
            const data = await ctx.service.template.findPublicTemplate(ctx.query);
            ctx.body = {
                code: 200,
                data,
                success: true,
                msg: ''
            }
        } catch (err) {
            throw err;
        }
    }

    /**
    * @Router GET /api/v1/public/templates:id
    * @Response 200 createTemplatesResponse 响应
    * @Summary 获取模板详情
    */
    async getTemplateDetail() {
        const ctx = this.ctx;
        try {
            const data = await ctx.service.template.findTemplateDetail(ctx.params.id);
            let color = {}
            let info = {}
            if (data) {
                color = await ctx.service.qiniu.getImageAve(data.imgUrl);
                info = await ctx.service.qiniu.getImageInfo(data.imgUrl);
            }

            ctx.body = {
                code: 200,
                data: { ...data, ...color, ...info },
                success: true,
                msg: ``
            };
        } catch (err) {
            throw err;
        }
    }
     
    /**
    * @Router GET /api/v1/public/templates:id/next
    * @Response 200 createTemplatesResponse 响应
    * @Summary 获取下一个模板的详情
    */
    async getNextTemplateDetail() {
        const ctx = this.ctx;
        try {
            const data = await ctx.service.template.findNextTemplateDetail(ctx.params.id);
            let color = {}
            let info = {}
            if (data) {
                color = await ctx.service.qiniu.getImageAve(data.imgUrl);
                info = await ctx.service.qiniu.getImageInfo(data.imgUrl);
            }

            ctx.body = {
                code: 200,
                data: { ...data, ...color, ...info },
                success: true,
                msg: ``
            };
        } catch (err) {
            throw err;
        }
    }

    async getUsers() {
        const ctx = this.ctx;
        try {
            const data = await ctx.service.user.findTodayUsers();
            ctx.body = {
                code: 200,
                data,
                success: true,
                msg: ''
            }
        } catch (err) {
            throw err;
        }
    }

    async getImageText() {
        const ctx = this.ctx;
        try {
            console.log(ctx.request.body);
            const data = await ctx.service.baidu.baiduOcr(ctx.request.body);
            ctx.body = {
                code: 200,
                data: JSON.parse(data.toString()),
                success: true,
                msg: ''
            }
        } catch (err) {
            throw err;
        }
    }


    /**
     * @Router GET /api/v1/public/oauth/token
     * @Response 200 baseResponse 响应
     * @Summary 获取accessToken
     */
    async getAccessToken() {
        const ctx = this.ctx;
        const data = await ctx.service.public.getAccessToken();
        try {
            ctx.body = {
                code: 200,
                data,
                success: true,
                msg: ''
            }
        } catch (err) {
            throw err;
        }
    }
}
module.exports = PublicController;
